package params

import (
	"errors"
	"micro-oauth/datamodels/enum"
	"strings"
)

type ClientListRequest struct {
	ListRequest
	PartnerId  uint64 `form:"partner_id" json:"partner_id"`
	ClientId   string
	ClientType enum.ClientType `form:"client_type" json:"client_type"`
	ScopeId    uint64          `form:"partner_id" json:"partner_id"`
	AllScope   bool
	Name       string `form:"name" json:"name"`
}

type ClientUpdateRequest struct {
	Name         string          `form:"name" json:"name"`
	Id           uint64          `form:"id" json:"id"`
	Secret       string          `form:"secret" json:"secret"`
	ClientId     string          `form:"client_id" json:"client_id"`
	RedirectURIs string          `form:"redirect_uris" json:"redirect_uris"`
	Type         enum.ClientType `form:"type" json:"type"`
	ScopeCodes   string          `form:"scope_codes" json:"scope_codes"`
}

func (r *ClientUpdateRequest) Validate() error {
	r.Name = strings.Trim(r.Name, " \t\n")
	r.Secret = strings.Trim(r.Secret, " \t\n")
	r.ClientId = strings.Trim(r.ClientId, "\t\n")
	r.RedirectURIs = strings.Trim(r.RedirectURIs, " \t\n")
	r.ScopeCodes = strings.Trim(r.ScopeCodes, " \t\n")
	if r.Name == "" || r.Secret == "" || r.RedirectURIs == "" {
		return errors.New("name, secret, redirect_uris required")
	}
	if len(r.ClientId) <= 10 {
		return errors.New("client_id length must greater than 10")
	}
	if len(r.Secret) <= 16 {
		return errors.New("secret length must greater than 16")
	}

	types := []enum.ClientType{enum.WebClient, enum.PosClient}
	ok := false
	for _, m := range types {
		if r.Type == m {
			ok = true
		}
	}
	if !ok {
		return errors.New("type invalid")
	}
	if r.ScopeCodes == "" {
		r.ScopeCodes = "hex-server"
	}

	return nil

}

type ClientCreateRequest struct {
	PartnerId    uint64          `form:"partner_id" json:"partner_id"`
	Name         string          `form:"name" json:"name"`
	Secret       string          `form:"secret" json:"secret"`
	ClientId     string          `form:"client_id" json:"client_id"`
	RedirectURIs string          `form:"redirect_uris" json:"redirect_uris"`
	Type         enum.ClientType `form:"type" json:"type"`
	ScopeCodes   string          `form:"scope_codes" json:"scope_codes"`
	ScopeId      uint64          `form:"scope_id" json:"scope_id"`
}

func (r *ClientCreateRequest) Validate() error {
	r.Name = strings.Trim(r.Name, " \t\n")
	r.Secret = strings.Trim(r.Secret, " \t\n")
	r.ClientId = strings.Trim(r.ClientId, "\t\n")
	r.RedirectURIs = strings.Trim(r.RedirectURIs, " \t\n")
	r.ScopeCodes = strings.Trim(r.ScopeCodes, " \t\n")
	if r.PartnerId == 0 {
		return errors.New("partner id can not be 0")
	}
	if r.Name == "" || r.Secret == "" || r.RedirectURIs == "" {
		return errors.New("name, secret, redirect_uris required")
	}
	if len(r.ClientId) <= 10 {
		return errors.New("client_id length must greater than 10")
	}
	if len(r.Secret) <= 16 {
		return errors.New("secret length must greater than 16")
	}

	types := []enum.ClientType{enum.WebClient, enum.PosClient}
	ok := false
	for _, m := range types {
		if r.Type == m {
			ok = true
		}
	}
	if !ok {
		return errors.New("type invalid")
	}
	if r.ScopeCodes == "" {
		r.ScopeCodes = "hex-server"
	}

	return nil
}
